package de.moc.client;

import java.util.Iterator;
import java.util.Vector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.moc.shared.*;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class SurveyCreator extends Composite {
	VerticalPanel	verticalPanel;
	TextBox		  	title;
	Vector<TextBox> choices;
	Button 		  	addChoice;
	Button		  	removeChoice;
	private Label lblTitle;
	private Label lblChoices;
	private HorizontalPanel horizontalPanel;
	
	public SurveyCreator() {
		choices = new Vector<TextBox>();
		verticalPanel = new VerticalPanel();
		
		lblTitle = new Label("Title");
		verticalPanel.add(lblTitle);
		title = new TextBox();
		title.setText("Please enter survey title");
		verticalPanel.add(title);
		title.setWidth("100%");
		
		horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");
		addChoice = new Button("Add choice");
		horizontalPanel.add(addChoice);
		addChoice.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				TextBox aBox = new TextBox();
				aBox.setWidth("100%");
				aBox.setText("Please enter choice description");
				choices.add(aBox);
				verticalPanel.add(aBox);
			}
		});
		addChoice.setWidth("100%");
		removeChoice = new Button("Remove choice");
		horizontalPanel.add(removeChoice);
		removeChoice.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				TextBox aBox = choices.lastElement();
				verticalPanel.remove(aBox);
				choices.remove(aBox);
			}
		});
		removeChoice.setWidth("100%");
		initWidget(verticalPanel);
		
		lblChoices = new Label("Choices");
		verticalPanel.add(lblChoices);
	}
	
	public Survey getSurvey() {
		Vector<Choice> choicesVector = new Vector<Choice>();
		
		for(Iterator<TextBox> it = choices.iterator();
			it.hasNext();) {
			String text = it.next().getText();
			choicesVector.add(new Choice(text));
		}
		
		return new Survey(title.getText(), choicesVector);
	}
}
